package com.newer.service.sys.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.newer.dao.sys.mapper.ResourceMapper;
import com.newer.pojo.sys.Resource;
import com.newer.service.sys.iter.ResourceService;
import com.newer.utils.sys.BootstrapDataTable;

@Service
public class ResourceServiceImpl implements ResourceService {

	/**
	 * 自动注入
	 */
	@Autowired
	private ResourceMapper resourceMapper;

	/**
	 * 显示资源列表
	 */
	@Override
	public BootstrapDataTable tableView(BootstrapDataTable bootstrapDataTable) {
		// 查询
		List<Resource> resourceList = resourceMapper.selectByExample(null);
		BootstrapDataTable bd = new BootstrapDataTable();
		// 一页的数据
		bd.setRows(resourceList);
		// 总记录数
		bd.setTotal(resourceList.size());
		return bd;
	}

	/**
	 * 级联删除
	 */
	@Override
	public boolean deleteResource(List<Integer> ids) {
		try {
			// 执行删除
			resourceMapper.deleteResource(ids);
			// 查询是否有子集
			List<Integer> list = resourceMapper.selectNodeIds(ids);
			if (list != null && !list.isEmpty()) {
				deleteResource(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			// 删除失败
			return false;
		}
		// true
		return true;
	}

}
